package za.co.varen.moke;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.ResourceBundle;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.SwingUtilities;
import org.apache.log4j.Logger;

public final class App implements Runnable {

    private ResourceBundle bundle = ResourceBundle.getBundle("moke");
    private JFrame container = new JFrame(bundle.getString("title.bar"));
    private JLayeredPane main = new JLayeredPane();
    private Preferences preferences = Preferences.userNodeForPackage(this.getClass());
    private static Logger log = Logger.getLogger(App.class);

    public static void main(String[] args){
        try {
            SwingUtilities.invokeAndWait(new App());
        } catch (InterruptedException ex) {
            log.error("Severe error running APP", ex);
        } catch (InvocationTargetException ex) {
            log.error("Severe error running APP", ex);
        }
    }

    public void run() {

        setupWindow();
        container.setVisible(true);
        container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    Dimension getWindowSize() {
        int width = toNumbers(preferences.get("window.size", null))[0];
        int height = toNumbers(preferences.get("window.size", null))[1];

        if (width == 0 & height == 0) {
            width = toNumbers(bundle.getString("default.size"))[0];
            height = toNumbers(bundle.getString("default.size"))[1];
        }
        return new Dimension(width, height);
    }

    Point getWindowLocation() {
        int width = toNumbers(preferences.get("window.location", null))[0];
        int height = toNumbers(preferences.get("window.location", null))[1];
        if (width == 0 & height == 0) {
            width = toNumbers(bundle.getString("default.location"))[0];
            height = toNumbers(bundle.getString("default.location"))[1];
        }
        return new Point(width, height);
    }

    private void setupWindow() {
        container.setSize(getWindowSize());
        container.addWindowListener(new CloseListener());
        container.setLocation(getWindowLocation());
        container.setLayout(new BorderLayout());
        container.add(main);
    }

    class CloseListener extends WindowAdapter {

        public void windowClosing(WindowEvent e) {
            String location = container.getLocation().x + "," + container.getLocation().y;
            preferences.put("window.location", location);
            String size = container.getSize().width + "," + container.getSize().height;
            preferences.put("window.size", size);
            try {
                preferences.flush();
            } catch (BackingStoreException ex) {
                log.info(ex.getLocalizedMessage());
            }
        }
    }

    private int[] toNumbers(String property) {
        String[] split = property.split(",");
        if (split.length > 1) {
            int[] ret = new int[split.length];
            for (int i = 0; i < split.length; i++) {
                ret[i] = Integer.parseInt(split[i]);

            }
            return ret;
        }
        return new int[]{0, 0};
    }
}
